//
//  RestAPI.swift
//  lookLikeIweekly
//
//  Created by ZhangXiaoqian on 16/6/5.
//  Copyright © 2016年 Mainzxq. All rights reserved.
//

import Foundation

let baseUrl = "http://apis.baidu.com/3023/news/channel"

enum NewsClass: String {
    case 热点 = "id=popular&page=2"
    case 推荐 = "id=recomm&page=2"
    case 娱乐 = "id=ent&page=2"
    case 社会 = "id=society&page=2"
    case 财经 = "id=finance&page=2"
    case 体育 = "id=sports&page=2"
    case 军事 = "id=mil&page=2"
    case 健康 = "id=health&page=2"
    case 科技 = "id=tech&page=2"
    case 汽车 = "id=auto&page=2"
    case 旅游 = "id=travel&page=2"
    case 历史 = "id=history&page=2"
    case 时尚 = "id=fashion&page=2"
    case 闺房 = "id=couple&page=2"
    case 游戏 = "id=game&page=2"
    case 互联网 = "id=internet&page=2"
    case 干货 = "id=real&page=2"
    case 国际 = "id=world&page=2"
    case 房产 = "id=house&page=2"
}






/*
 {
 "id": "popular",
 "channel": "热点"
	},
	{
 "id": "recomm",
 "channel": "推荐"
	},
	{
 "id": "ent",
 "channel": "娱乐"
	},
	{
 "id": "society",
 "channel": "社会"
	},
	{
 "id": "finance",
 "channel": "财经"
	},
	{
 "id": "sports",
 "channel": "体育"
	},
	{
 "id": "mil",
 "channel": "军事"
	},
	{
 "id": "health",
 "channel": "健康"
	},
	{
 "id": "tech",
 "channel": "科技"
	},
	{
 "id": "auto",
 "channel": "汽车"
	},
	{
 "id": "travel",
 "channel": "旅游"
	},
	{
 "id": "history",
 "channel": "历史"
	},
	{
 "id": "fashion",
 "channel": "时尚"
	},
	{
 "id": "couple",
 "channel": "闺房"
	},
	{
 "id": "game",
 "channel": "游戏"
	},
	{
 "id": "internet",
 "channel": "互联网"
	},
	{
 "id": "real",
 "channel": "干货"
	},
	{
 "id": "world",
 "channel": "国际"
	},
	{
 "id": "house",
 "channel": "房产"
	},
	{
 "id": "edu",
 "channel": "教育"
	},
	{
 "id": "baby",
 "channel": "育儿"
	},
	{
 "id": "anecdote",
 "channel": "奇闻"
	},
	{
 "id": "food",
 "channel": "美食"
	},
	{
 "id": "antique",
 "channel": "文玩"
	},
	{
 "id": "astro",
 "channel": "星座"
	},
	{
 "id": "comic",
 "channel": "动漫"
	},
	{
 "id": "shares",
 "channel": "股票"
	},
	{
 "id": "nba",
 "channel": "NBA"
	},
	{
 "id": "home",
 "channel": "家居"
	},
	{
 "id": "abroad",
 "channel": "留学"
	},
	{
 "id": "beauty",
 "channel": "美容"
	},
	{
 "id": "digital",
 "channel": "数码"
	}
 
 */



func getDataFromRest(url: String) -> NSDictionary {
    var dict = [:]
    let nsUrl = NSURL(string: url)
    let req = NSMutableURLRequest(URL: nsUrl!)
    req.HTTPMethod = "GET"
    req.timeoutInterval = 6
    req.addValue("b93117cab57dac8d1412036532a76743", forHTTPHeaderField: "apikey")
    let session = NSURLSession.sharedSession()
    let semaphore = dispatch_semaphore_create(0)
    session.dataTaskWithRequest(req) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
        if error == nil {
            do {
            dict = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! NSDictionary
            } catch {
                print("something bad happened")
            }
            dispatch_semaphore_signal(semaphore)
        }
    }.resume()
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
    return dict
}


func postDataToRest(url: String, params: NSDictionary) {
    let nsUrl = NSURL(string: url)
    let req = NSMutableURLRequest(URL: nsUrl!)
    req.HTTPMethod = "PATCH"
    req.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    do {
        // data 需要从字典型转换成json
        req.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: .PrettyPrinted)
    } catch {
        "something bad"
    }
    let semaphore = dispatch_semaphore_create(0)
    let session = NSURLSession.sharedSession()
    session.dataTaskWithRequest(req) { (data: NSData?, response: NSURLResponse?, error: NSError?) in
        guard let sureResponse = response as? NSHTTPURLResponse where
            sureResponse.statusCode == 200 else {
                print("no good")
                return
        }
        dispatch_semaphore_signal(semaphore)
    }.resume()
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
}
